// pages/test/test.js
import {
  request
} from "../../request/index.js";

const goodsStatus = {
  out: 0,
  in: 1,
  done: 2
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    ruleShow: false,
    url:'',
    orders: [],
    // 待发货的数组
    ordersOut: [],
    // 待收货的数组
    ordersIn: [],
  },

  // 唤起弹窗
  outPut() {
    this.setData({
      show: !this.data.show
    })
  },
   // 唤起弹窗
   rulePut() {
    this.setData({
      ruleShow: !this.data.ruleShow
    })
  },

  jmp_ad(){
    wx.navigateTo({
      url: `../../pages/advertisement/advertisement?url=https://mp.weixin.qq.com/s/Knniad3zWd4RVwpQ31v50A`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  loginOn(){
    wx.login({
      timeout: 20000,
      success(res) {
        wx.request({
          url: 'https://dogborther.top/user/login',
          
          data: {
            code: res.code
          },
          success(res1) {
            wx.setStorageSync('token', res1.data.data.token)
            console.log(res1, "***********");
          }, fail(err) {
            console.log(err);
          }
        })
      }
    })
  },
  sendMessage(event){
    wx.requestSubscribeMessage({
      tmplIds:['97h4zg0WlvLj5Qy4SXpgxJz9ztlcU3aeVek8tGpzFYM'],
      success(res){
        console.log(res);
        request({
          url:"/message/send",
          method:"POST",
          data:{
            "address": "string",
            "bookName": "string",
            "phoneNum": "136896908922",
            "tradeNo": "string1"
          }
        }).then(res1=>{
          console.log(res1);
        }).catch(err=>{
          console.log(err);
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    request({ url: '/userPage/getBuy' })
      .then(result => {
        console.log(result)
        let listOut = []
        let listIn = []
        let list = result.data.data.list
        list.forEach((r) => {
          r.select = 'circle';
          // 待发货
          if (r.tradeState === goodsStatus.out) {
            listOut.push(r)
            r.status = '待发货'
          }
          // 已发货 待收货
          else if (r.tradeState === goodsStatus.in) {
            listIn.push(r)
            r.status = '待收货'
          }
          // 已完成
          else if (r.tradeState === goodsStatus.done) {
            r.status = '已完成'
          }
        })
        console.log(list)
        that.setData({
          ordersOut: listOut,
          ordersIn: listIn,
          orders: list,
          // count: result.data.data.list.length
        })
        console.log(this.data.orders)
        console.log(this.data.ordersOut)
        console.log(this.data.ordersIn)
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})